﻿using System;
using System.Collections.Generic;
using System.Text;

using Torch.WL.Entity.CleanChina;
using System.Data.SqlClient;
using System.Data;

namespace Torch.WL.DataAccess.CleanChina
{
    public class TbForumAccess
    {
        public TbForumAccess()
        {

        }

        public List<TbForumEntity> QueryForum(int onlygettodayforum,string querykey,
            int timesort,
            int timesorttype,
            int classsort,
            int classsorttype
            )
        {
            SqlParameter[] parameters = {
					new SqlParameter("@onlygettodayforum", onlygettodayforum),
                    new SqlParameter("@querykey", querykey),
                    new SqlParameter("@timesort", timesort),
                    new SqlParameter("@timesorttype", timesorttype),
                    new SqlParameter("@classsort", classsort),
                    new SqlParameter("@classsorttype", classsorttype)
			};

            DataSet ds = DbHelperSQL.RunProcedure("twsp_Forum_Query", parameters, "ds");

            return DataTableToList(ds.Tables[0]);

        }

        public TbForumEntity GetModel(int forumId)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("select  top 1 * from Tb_Forum ");
            strSql.Append(" where IsLeafNode=1 and forumId=@forumId");
            SqlParameter[] parameters = {
					new SqlParameter("@forumId", SqlDbType.Int,4)
			};
            parameters[0].Value = forumId;

            TbForumEntity model = new TbForumEntity();
            DataSet ds = DbHelperSQL.Query(strSql.ToString(), parameters);
            if (ds.Tables[0].Rows.Count > 0)
            {
                return DataRowToModel(ds.Tables[0].Rows[0]);
            }
            else
            {
                return null;
            }
        }


        private TbForumEntity DataRowToModel(DataRow row)
        {
            TbForumEntity model = new TbForumEntity();
            if (row != null)
            {
                if (row["forumId"] != null && row["forumId"].ToString() != "")
                {
                    model.forumId = int.Parse(row["forumId"].ToString());
                }
                if (row["forumPId"] != null && row["forumPId"].ToString() != "")
                {
                    model.forumPId = int.Parse(row["forumPId"].ToString());
                }
                if (row["forumName"] != null)
                {
                    model.forumName = row["forumName"].ToString();
                }
                if (row["IsLeafNode"] != null && row["IsLeafNode"].ToString() != "")
                {
                    if ((row["IsLeafNode"].ToString() == "1") || (row["IsLeafNode"].ToString().ToLower() == "true"))
                    {
                        model.IsLeafNode = true;
                    }
                    else
                    {
                        model.IsLeafNode = false;
                    }
                }
                if (row["forumType"] != null && row["forumType"].ToString() != "")
                {
                    model.forumType = int.Parse(row["forumType"].ToString());
                }
                if (row["SortIndex"] != null && row["SortIndex"].ToString() != "")
                {
                    model.SortIndex = int.Parse(row["SortIndex"].ToString());
                }
                if (row["PIdPath"] != null)
                {
                    model.PIdPath = row["PIdPath"].ToString();
                }
                if (row["showDate"] != null)
                {
                    model.showDate = row["showDate"].ToString();
                }
                if (row["BeginDate"] != null && row["BeginDate"].ToString() != "")
                {
                    model.BeginDate = DateTime.Parse(row["BeginDate"].ToString());
                }
                if (row["EndDate"] != null && row["EndDate"].ToString() != "")
                {
                    model.EndDate = DateTime.Parse(row["EndDate"].ToString());
                }
                if (row["Place"] != null)
                {
                    model.Place = row["Place"].ToString();
                }
                if (row["forumTeacher"] != null)
                {
                    model.forumTeacher = row["forumTeacher"].ToString();
                }
                if (row["forumTheme"] != null)
                {
                    model.forumTheme = row["forumTheme"].ToString();
                }
                if (row["Memo"] != null)
                {
                    model.Memo = row["Memo"].ToString();
                }
                if (row["MemoImg"] != null)
                {
                    model.MemoImg = row["MemoImg"].ToString();
                }
                if (row["raceZanZhu"] != null)
                {
                    model.raceZanZhu = row["raceZanZhu"].ToString();
                }
                if (row["racePerson"] != null)
                {
                    model.racePerson = row["racePerson"].ToString();
                }
                if (row["createTime"] != null && row["createTime"].ToString() != "")
                {
                    model.createTime = DateTime.Parse(row["createTime"].ToString());
                }
            }
            return model;
        }


        private List<TbForumEntity> DataTableToList(DataTable dt)
        {
            List<TbForumEntity> modelList = new List<TbForumEntity>();
            int rowsCount = dt.Rows.Count;
            if (rowsCount > 0)
            {
                TbForumEntity model;
                for (int n = 0; n < rowsCount; n++)
                {
                    model = DataRowToModel(dt.Rows[n]);
                    if (model != null)
                    {
                        modelList.Add(model);
                    }
                }
            }
            return modelList;
        }

    }
}
